package org.platform.lwc.mybatis.support;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.Objects;

/**
 * 分页工具
 *
 * @author lwc
 * @date 2020/12/28
 */
public class Condition {

	/**
	 * 转化成mybatis plus中的Page
	 *
	 * @param query
	 * @return
	 */
	public static <T> IPage<T> getPage(Query query) {
		if (Objects.isNull(query)){
			query = new Query();
		}
		IPage<T> page = new Page<>(Objects.nonNull(query.getCurrent())?query.getCurrent():1, Objects.nonNull(query.getCurrent())?query.getSize():10);
		return page;
	}


	/**
	 * 获取mybatis plus中的QueryWrapper
	 *
	 * @param entity
	 * @param <T>
	 * @return
	 */
	public static <T> QueryWrapper<T> getQueryWrapper(T entity) {
		return new QueryWrapper<>(entity);
	}

	public static <T> UpdateWrapper<T> getUpdateWrapper(T entity) {
		return new UpdateWrapper<>(entity);
	}
}
